#!/bin/sh
appBin=`dirname $0`
cd $appBin
basepath=`pwd`
appLog=$basepath/../log
appConf=$basepath/../conf
appLib=$basepath/../lib
ppPath=$basepath/../pinpointAgent

jpacfg=""

if [ -n "${shtelpaas_jpa_entity_basepkg}" ]; then
	jpacfg=" --shtelpaas.jpa.entity.basepkg=${shtelpaas_jpa_entity_basepkg} "
fi

if [ -n "${shtelpaas_jpa_repo_basepkg}" ]; then
	jpacfg=${jpacfg}" --shtelpaas.jpa.repo.basepkg=${shtelpaas_jpa_repo_basepkg} "
fi

pinpoint=""

if [ -d "$ppPath" -a -n "${profiler_collector_ip}"  ]; then
	#pinpoint文件不支持环境变量，人工替换
	sed -i "s/\${profiler_collector_ip}/${profiler_collector_ip}/g" $ppPath/pinpoint.config
	
	if [ -n "${profiler_shtelpaas_pp_enable}" ]; then
		sed -i "s/\${profiler_shtelpaas_pp_enable}/${profiler_shtelpaas_pp_enable}/g" $ppPath/pinpoint.config
	else
		sed -i "s/\${profiler_shtelpaas_pp_enable}/false/g" $ppPath/pinpoint.config
	fi
	
	if [ -z "${profiler_shtelpaas_pp_joinpoint}" ]; then
		profiler_shtelpaas_pp_joinpoint="profiler.shtelpaas.pp.class.com.shtel.paas.sdk.core.log.PaasLogger._method_.metricInfo=java.lang.String,java.lang.Object[]"
	fi
	sed -i "s/\${profiler_shtelpaas_pp_joinpoint}/${profiler_shtelpaas_pp_joinpoint}/g" $ppPath/pinpoint.config
	
	if [ -n "${profiler_include}" ]; then
		sed -i "s/\${profiler_include}/${profiler_include}/g" $ppPath/pinpoint.config
	else
		sed -i "s/\${profiler_include}//g" $ppPath/pinpoint.config
	fi
	
	if [ -n "${profiler_entrypoint}" ]; then
		sed -i "s/\${profiler_entrypoint}/${profiler_entrypoint}/g" $ppPath/pinpoint.config
	else
		sed -i "s/\${profiler_entrypoint}//g" $ppPath/pinpoint.config
	fi
	
	if [ -n "${profiler_sampling_rate}" ]; then
		sed -i "s/\${profiler_sampling_rate}/${profiler_sampling_rate}/g" $ppPath/pinpoint.config
	else
		sed -i "s/\${profiler_sampling_rate}/1/g" $ppPath/pinpoint.config
	fi
	
	if [ -n "${pinpoint_agentId}" ]; then
		agentId="${pinpoint_agentId}"
	else
		#pinpoint.agentId can only contain [a-zA-Z0-9], '.', '-', '_'. maxLength:24
		agentId=`hostname`${shtelpaas_app_name}
		agentId=${agentId//./}
		agentId=${agentId//_/}
		agentId=${agentId//-/}
		let stIndex=${#agentId}-24
		if [ "${stIndex}" -lt 0 ]; then
			stIndex=0
		fi
		agentId=${agentId:stIndex}
	fi
	
	if [ -n "${pinpoint_applicationName}" ]; then
		ppApp="${pinpoint_applicationName}"
	else
		ppApp="${shtelpaas_app_name}"
	fi
	
	pinpoint="-javaagent:$ppPath/pinpoint-bootstrap-1.6.2.jar -Dpinpoint.agentId=${agentId} -Dpinpoint.applicationName=${ppApp}"
fi

ipinfo=""

if [ -n "${shtelpaas_app_ip}" ]; then
	ipinfo="-Dappip=${shtelpaas_app_ip}"
fi

busiCenter=""

if [ -n "${shtelpaas_center_name}" ]; then
	busiCenter="-DbusiCenter=${shtelpaas_center_name}"
fi

serviceLayer=""

if [ -n "${shtelpaas_service_layer}" ]; then
	serviceLayer="-DservType=${shtelpaas_service_layer}"
fi

dockerId=""

if [ -n "${HOSTNAME}" ]; then
	dockerId="-Dpinpoint.dockerId=${HOSTNAME}"
fi

localhostIp=""

if [ -n "${paas_container_host_ip}" ]; then
	localhostIp="-Dpinpoint.localhostIp=${paas_container_host_ip}"
fi

e2eAgent=""

if [ -n "${ETE_AGENT_ENABLE}" -a -n "${ETE_AGENT_JAR}" -a -n "${ETE_AGENT_YML}" ]; then
	e2eAgent="-javaagent:${ETE_AGENT_JAR}=${ETE_AGENT_YML}"
fi

java ${JAVA_OPTS}  -cp "$appLib/*" -Dlogpath=$appLog -Dappname=${shtelpaas_app_name} ${busiCenter} ${serviceLayer} ${ipinfo} ${dockerId} ${localhostIp} ${pinpoint} ${e2eAgent} com.shtel.paas.sdk.app.PaasMainApp  --shtelpaas.app.name=${shtelpaas_app_name} --shtelpaas.app.basepkg=${shtelpaas_app_basepkg} --shtelpaas.app.config.profile=${shtelpaas_app_config_profile} --shtelpaas.log.profile=${shtelpaas_log_profile}  --shtelpaas.app.nameserver=${shtelpaas_app_nameserver} ${jpacfg}